<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Breadcrumb Library
 *
 * @package		SimplyPost
 * @subpackage	Libraries
 * @author		Pascal Kriete
 */

class Breadcrumb {
	
	var $_CI;
	var $_path;
	var $_delim;
	
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function Breadcrumb()
	{
		$this->_CI =& get_instance();
		
		// Change this if you want a different delimiter
		$this->_delim = ' &rarr ';
		
		// If you want to prepend something (i.e. homepage link) do it
		// in the _output() function
		$this->_path = '';
	}

	// --------------------------------------------------------------------
	
	/**
	 * Generate a breadcrumb by going up through the database
	 *
	 * @access	public
	 * @param	integer	id of the starting topic/forum/category
	 * @param	string	type of starting point id ('topic', 'forum', etc)
	 * @param	string	anything to append?
	 * @return	void
	 */
	function from_db($id = -1, $type = '', $slug = '')
	{
		// The keys make up the options for $type
		$tables = array(
			'category'	=> 'categories',
			'forum'		=> 'forums',
			'topic'		=> 'topics',
			'post'		=> 'posts',
			'member'	=> 'users'
		);
		
		// Field parents so it can move up
		$parents = array(
			'posts'		=> 'topics',
			'topics'	=> 'forums',
			'forums'	=> 'categories'
		);
				
		// Bogus type?  Not going through the db then
		if (! array_key_exists($type, $tables))
			$this->from_path($slug);

		$start_table = $tables[$type];

		// Member, or not a member?
		if ($type == 'member')
		{
			$route =& load_class('Router');
			
			$link = base_url() . $route->directory;
			$this->_path .= $this->_delim.anchor($link, ucfirst($route->directory));

			if ($route->class != $route->directory)
			{
				$link .= '/'.$route->class;
				$this->_path .= $this->_delim.anchor($link, ucfirst($route->class));
			}
			
			$row = $this->_db_query($tables[$type], $id);
			$this->_path .= $this->_create_anchor($start_table, $id, $row['username']);
		}
		else
		{			
			// Runs while the returned row has a parent id
			while( array_key_exists('parent_id', $row = $this->_db_query($start_table, $id)) )
			{
				// Some things don't have names
				if (!array_key_exists('name', $row))
					$row['name'] = ucfirst($type).' '.$id;
				
				// Current doesn't get a link
				if ($start_table == $tables[$type])
					$this->_path = $this->_delim . ucfirst($row['name']) . $this->_path;
				else
					$this->_path = $this->_create_anchor($start_table, $id, $row['name']) . $this->_path;
					
				$id = $row['parent_id'];
				$start_table = $parents[$start_table];
			}
			
			// The last query was run, but never added because it doesn't have a parent
			$this->_path  = 	$this->_create_anchor($start_table, $id, $row['name']) . $this->_path;
		}
		
		$this->_path .= 	( $slug !== '' ) ? $this->_delim . $slug : '';
		$this->_output();
	}

	// --------------------------------------------------------------------
	
	/**
	 * Generate a breadcrumb by going up through the directory structure
	 *
	 * @access	public
	 * @param	string
	 * @return	void
	 */
	function from_path($slug = '')
	{
		$link = base_url();
		$route =& load_class('Router');
		
		if ($route->directory != 'home')
		{
			$link .= $route->directory;
			$this->_path .= $this->_delim.anchor($link, ucfirst($route->directory));
		}
		
		if ($route->class != $route->directory)
		{
			$link .= '/'.$route->class;
			$this->_path .= $this->_delim.anchor($link, ucfirst($route->class));
		}
		
		if ($route->method != 'index')
		{
			$link .= '/'.$route->method;
			$this->_path .= $this->_delim.anchor($link, ucfirst($route->method));
		}
		
		$this->_path .= $slug !== '' ? $this->_delim . $slug : '';
		$this->_output();
	}

	// --------------------------------------------------------------------
	
	/**
	 * Generate a breadcrumb from an array of custom keys
	 *
	 * @access	public
	 * @param	array  Array filled with custom keys
	 * @return	void
	 */
	function custom($parts = array())
	{
		foreach($parts as $part)
		{
			$this->_path .= $this->_delim . $part;
		}
		$this->_output();
	}

	// --------------------------------------------------------------------
	
	/**
	 * Adds the breadcrumb to the output buffer
	 *
	 * @access	private
	 * @return	void
	 */
	function _output()
	{
		ob_start();
		
		echo '<div id="crumb">';
		echo anchor(base_url(), 'Forum') . $this->_path;
		echo '</div>';
		
		// Add to the global buffer
		$output =& load_class('Output');
		$output->append_output(ob_get_contents());
		@ob_end_clean();
	}

	// --------------------------------------------------------------------
	
	/**
	 * Queries the database and returns result row
	 *
	 * @access	private
	 * @param	string	name of the database table
	 * @param	integer	id of the row
	 * @return	mixed
	 */
	function _db_query($table, $id)
	{
		$query = $this->_CI->db->get_where($table, array('id' => $id), 1, 0);
		if ($query->num_rows() > 0)
			return $query->row_array();
		else
			return FALSE;
	}

	// --------------------------------------------------------------------
	
	/**
	 * Creates clean anchor
	 *
	 * @access	private
	 * @param	string	name of the database table
	 * @param	integer	id of the topic/forum/category
	 * @param	string	anchor text
	 * @return	string	finished anchor
	 */
	function _create_anchor($table, $id, $name)
	{
		// Reverse lookup ;)
		$tables = array(
			'categories'	=> 'category',
			'forums'		=> 'forum',
			'topics'		=> 'topic',
			'posts'			=> 'post',
			'users'			=> 'member/profile'
		);
		
		return $this->_delim . anchor( $tables[$table].'/view/'.$id, ucfirst($name));
	}
	
}

?>